package pers.qianyu.month_202103.date_20210307.contest_231;

import org.junit.*;

/**
 * 5698. 构成特定和需要添加的最少元素
 * https://leetcode-cn.com/problems/minimum-elements-to-add-to-form-a-given-sum/
 *
 * @author mizzle rain
 * @date 2021-03-07 10:47
 */
public class MinElements {
    public int minElements(int[] nums, int limit, int goal) {
        long sum = 0;
        for (int x : nums) sum += x;
        long target = goal - sum;
        if (target < 0) target = -target;
        return (int) ((target + limit - 1) / limit);
    }

    @Test
    public void test1() {
        int r = new MinElements().minElements(new int[]{1, -1, 1}, 3, -4);
        Assert.assertEquals(2, r);
    }

    @Test
    public void test2() {
        int r = new MinElements().minElements(new int[]{1, -10, 9, 1}, 100, 0);
        Assert.assertEquals(1, r);
    }
}
